package com.baseframe.match.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baseframe.match.entity.SnsMatch;
import com.baseframe.match.entity.request.SnsMatchRequest;
import com.baseframe.match.entity.vo.SnsMatchVO;
import com.baseframe.match.service.SnsMatchService;
import com.baseframe.mybatis.utils.BaseMetaObjectUtil;
import com.baseframe.tool.result.R;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/back/match")
@AllArgsConstructor
public class BackSnsMatchController {

    private SnsMatchService snsMatchService;

    /**
     * 新增或修改竞赛
     *
     * @param request 请求对象，包含竞赛信息
     * @return 返回操作结果
     */
    @PostMapping("/save-or-update")
    public R<Object> add(@RequestBody SnsMatchRequest request) {
        SnsMatch snsMatch = new SnsMatch();
        BeanUtils.copyProperties(request, snsMatch,
                BaseMetaObjectUtil.getUpdateFillPropertiesName(snsMatch.getClass()));

        return snsMatchService.saveOrUpdate(snsMatch) ? R.success() : R.fail();
    }

    /**
     * 分页查询竞赛列表
     *
     * @param page    分页对象
     * @param request 请求对象，包含查询条件
     * @return 返回竞赛信息的分页结果
     */
    @GetMapping("/page")
    public R<Page<SnsMatchVO>> matchPageList(Page<SnsMatch> page, SnsMatchRequest request) {
        return R.data(snsMatchService.matchPageList(page, request));
    }

    /**
     * 删除竞赛
     *
     * @param request 请求对象，包含竞赛ID
     * @return 返回操作结果
     */
    @PostMapping("/delete")
    public R<Object> delete(@RequestBody SnsMatchRequest request) {
        if (request.getId() == null) {
            return R.fail("缺少必要请求参数~");
        }

        return snsMatchService.removeById(request.getId()) ? R.success() : R.fail();
    }
}
